<template>
  <div class="login-page">
    <el-card class="el-card">
      <div class="logo"></div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="loginName">
          <el-input
            v-model="form.loginName"
            placeholder="请输入用户名"
            prefix-icon="el-icon-mobile"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            ref="password"
            v-model="form.password"
            :type="inputType"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          >
            <template #suffix>
              <el-button
                :icon="
                  inputType === 'password' ? 'el-icon-view' : 'el-icon-delete'
                "
                type="text"
                @click="changepow"
              ></el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="form.code"
            placeholder="请输入验证码"
            prefix-icon="el-icon-price-tag"
          >
            <template #suffix>
              <img
                ref="img"
                :src="png"
                height="40px"
                style="width: 100px"
                @click="getImg"
              />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            class="add"
            type="primary"
            @click="submit"
            >登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { codeApi } from "@/api/user";
import store from "@/store";

export default {
  name: "LoginIndex",

  data() {
    return {
      form: {
        loginName: "admin",
        password: "admin",
        clientToken: 1,
        loginType: 0,
        mobile: "",
        code: "",
      },
      success: "",
      png: "",
      inputType: "password",
      rules: {
        loginName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
      loading: false,
    };
  },
  created() {
    this.getImg();
  },
  methods: {
    async getImg() {
      const res = await codeApi(1);
      this.$refs.img.src = window.URL.createObjectURL(res);
    },
    async submit() {
      try {
        this.loading = true;
        await this.$refs.form.validate();
        await this.$store.dispatch("user/loginAction", this.form);
        if (store.state.user.userInfo.success === false) {
          this.$message.error(store.state.user.userInfo.msg);
          this.loading = false;
          return 1;
        } else {
          this.$router.push("/");
          this.$message.success("登陆成功");
        }
      } finally {
        this.loading = false;
      }
    },
    changepow() {
      this.inputType === "password"
        ? (this.inputType = "text")
        : (this.inputType = "password");
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  //background-color: #2b4b6b;
  background-image: url("https://likede2-admin.itheima.net/img/background.be4fae7d.png");
  display: flex;
  justify-content: center;
  align-items: center;

  .el-card {
    width: 450px;
    height: 350px;
    position: relative;
    overflow: visible;

    .logo {
      overflow: visible;
      width: 130px;
      height: 130px;
      border-radius: 50%;
      background: url("https://likede2-admin.itheima.net/img/logo.595745bd.png")
        center/cover;
      position: absolute;
      z-index: 3;
      top: -25%;
      left: 35%;
    }

    .el-form {
      padding-top: 70px;

      .canvas {
        height: 40px;
      }

      .svg-container {
        z-index: 3;
      }

      .add {
        width: 410px;
        background-color: #4e69e7;
      }
    }
  }
}
</style>
